<?php
ini_set('date.timezone','Asia/Shanghai');
//error_reporting(E_ERROR);
require_once "../lib/WxPay.Api.php";
require_once "WxPay.JsApiPay.php";
require_once 'log.php';
$user_id=$_GET['user_id'];
$order_id=$_GET['order_id'];
$mysql_server_name='hdm373779577.my3w.com'; //改成自己的mysql数据库服务器
$mysql_username="hdm373779577"; //改成自己的mysql数据库用户名
$mysql_password="nobaogroup2018"; //改成自己的mysql数据库密码
$mysql_database="hdm373779577_db"; //改成自己的mysql数据库名
@$conn=new mysqli($mysql_server_name,$mysql_username,$mysql_password); //连接数据库
@$conn->query("set names ‘utf8‘"); //数据库输出编码 应该与你的数据库编码保持一致.南昌网站建设公司百恒网络PHP工程师建议用UTF-8 国际标准编码.
@$conn -> select_db($mysql_database); //打开数据库
@$sql ="select money,order_number from nbsm_order WHERE state_del=1 and  user_id=".$user_id." and id=".$order_id; //SQL语句
@$result = $conn->query($sql); //查询成功
$order_info=$result->fetch_assoc();
$conn->close();//关闭连接
//print_r($order_info);die;
//初始化日志
$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);

//打印输出数组信息
/*function printf_info($data)
{
    foreach($data as $key=>$value){
        echo "<font color='#00ff55;'>$key</font> : $value <br/>";
    }
}*/

//①、获取用户openid
$tools = new JsApiPay();
$openId = $tools->GetOpenid();
//②、统一下单
$input = new WxPayUnifiedOrder();
$input->SetBody("挪丹瑞芬");
$attach=base64_encode(json_encode(['user_id'=>$user_id,'order_id'=>$order_id,'order_number'=>$order_info['order_number']]));
$input->SetAttach($attach);
$input->SetOut_trade_no($order_info['order_number']);
$input->SetTotal_fee($order_info['money']*100);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
//$input->SetGoods_tag("test");
$input->SetNotify_url("http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/Wxgzhpay/notify");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = WxPayApi::unifiedOrder($input);
// printf_info($order);
$jsApiParameters = $tools->GetJsApiParameters($order);

//获取共享收货地址js函数参数
$editAddress = $tools->GetEditAddressParameters();

//③、在支持成功回调通知中处理成功之后的事宜，见 notify.php
/**
 * 注意：
 * 1、当你的回调地址不可访问的时候，回调通知会失败，可以通过查询订单来确认支付是否成功
 * 2、jsapi支付时需要填入用户openid，WxPay.JsApiPay.php中有获取openid流程 （文档可以参考微信公众平台“网页授权接口”，
 * 参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html）
 */
?>

<html>
<head>
    <meta charset="UTF-8">
	<title>挪丹瑞芬确认支付</title>
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
	<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
	<meta content="yes" name="apple-mobile-web-app-capable">
	<meta content="black" name="apple-mobile-web-app-status-bar-style">
	<meta content="telephone=no" name="format-detection">
    <link rel="stylesheet" href="../../css/mine.css">
</head>
<body>
<header>
		<a href="../../index.html">
			<div id="back" class="back">
				&lt; 返回首页
			</div>
		</a>
		支付
	</header>
<div class="pay">
        <i class="pay-icon"></i>
        <div class="right">
            <div class="order-msg">订单已生成 等待支付</div>
            <div class="order-pay">共需支付
                <div class="order-price">￥<?php echo $order_info['money']?></div>
            </div>
        </div>
        <div class="pay-btn-box">
            <a id="getBrandWCPayRequest" href="#">
                <p class="pay-btn" onclick="callpay()">立即支付</p>
            </a>
        </div>
    </div>
<script type="text/javascript">
        //调用微信JS api 支付
        function jsApiCall()
        {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest',
                <?php echo $jsApiParameters; ?>,
                function(res){
                    if (res.err_msg === 'get_brand_wcpay_request:ok') {
                        alert('支付成功，返回订单列表！');
                        window.location.href="http://www.importndsf.com/html/pay_success.html"
                    } else if (res.err_msg === 'get_brand_wcpay_request:cancel') {
                        alert('取消支付！');
                        window.location.href="http://www.importndsf.com/html/pay_success.html"
                    }
                }
            );
        }

        function callpay()
        {
            if (typeof WeixinJSBridge == "undefined"){
                if( document.addEventListener ){
                    document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                }else if (document.attachEvent){
                    document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                    document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                }
            }else{
                jsApiCall();
            }
        }
    </script>
</body>
</html>
